<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 150px;"
        class="filter-item"
        placeholder="请输入反馈人名称"
        v-model="queryForm.userName"
      >
      </el-input>
      <el-input
        style="width: 150px;"
        class="filter-item"
        placeholder="请输入答复人名称"
        v-model="queryForm.answerName"
      >
      </el-input>
      <el-select
        clearable
        style="width: 120px"
        class="filter-item"
        v-model="queryForm.status"
        placeholder="请选择状态"
      >
        <el-option
          v-for="(val, key) in typeOptions"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
    </div>
    <el-table v-loading="loading" :data="records" style="width: 100%">
      <el-table-column
        prop="userName"
        label="反馈人"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="feedbackTime"
        label="反馈时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="linkPhone"
        label="联系方式"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="answerName"
        label="答复人"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="answerTime"
        label="答复时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="status" label="状态" width="150" align="center">
        <template slot-scope="scope">
          {{ Dicts.HANDLING_STATUS[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="modifyTime"
        label="修改时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
        width="250"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'feedBack:answer'"
            size="mini"
            type="primary"
            @click="editFeedback(scope.row)"
            title="答复"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            v-permission="'feedBack:detail'"
            size="mini"
            @click="showFeedback(scope.row)"
            title="详情"
            icon="el-icon-search"
            circle
          ></el-button>
          <el-button
            v-permission="'feedBack:delete'"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            title="删除"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="'问题反馈详情'"
      :visible.sync="dialogShowFormVisible"
      width="50%"
    >
      <el-form
        :model="showForm"
        label-width="160px"
        :rules="showFormRules"
        ref="showForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="应用版本号:" prop="appVersion">
              {{ showForm.appVersion }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="应用类型:" prop="appType">
              {{ Dicts.APP_TYPE[showForm.appType] }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="客户端操作系统:" prop="clientType">
              {{ Dicts.CLIENT_TYPE[showForm.clientType] }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="问题分类:" prop="classCode">
              {{ showForm.classCode }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="问题类型:" prop="typeCode">
              {{ showForm.typeCode }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="分辨率:" prop="resolution">
              {{ showForm.resolution }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="问题描述:" prop="questionDesc">
              {{ showForm.questionDesc }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" v-if="urls !== undefined && urls.length !== 0">
            <el-form-item label="附件:" prop="urls">
              <div v-viewer>
                <img
                  :src="$oss(item)"
                  alt
                  style="width:30px;height:30px;border-radius:5px;"
                  v-for="item in urls"
                  :key="item.id"
                  v-if="item"
                />
              </div>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="答复内容:" prop="answerDesc">
              {{ showForm.answerDesc }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="备注:" prop="remark">
              {{ showForm.remark }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogShowFormVisible = false">关 闭</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="'问题反馈答复'"
      :visible.sync="dialogEditFormVisible"
      width="50%"
    >
      <el-form
        :model="editForm"
        label-width="160px"
        :rules="showEditRules"
        ref="editForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="问题描述:" prop="questionDesc">
              {{ editForm.questionDesc }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="答复内容" prop="answerDesc">
              <el-input
                type="textarea"
                v-model="editForm.answerDesc"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="execEditForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryFeedback,
  deleteFeedback,
  answerFeedback,
  queryAttach
} from '@/api/terminal'
import 'viewerjs/dist/viewer.css'
import Viewer from 'v-viewer'
import Vue from 'vue'
Vue.use(Viewer)

export default {
  name: 'feedback',
  data() {
    return {
      dialogShowFormVisible: false,
      dialogEditFormVisible: false,
      loading: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        status: undefined,
        title: undefined
      },
      showFormRules: {},
      showForm: {},
      showEditRules: {
        answerDesc: [
          { required: true, message: '请输入答复内容', trigger: 'blur' }
        ]
      },
      editForm: {},
      typeOptions: Dicts.HANDLING_STATUS,
      records: [],
      urls: []
    }
  },
  created() {
    // 初始化一次oss client,如果初始化过一次返回上一次初始化对象
    this.initOssClient().then(_ => {
      this.doQuery()
    })
  },
  methods: {
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryFeedback(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    handleDelete(rowId) {
      this.$confirm('此操作将删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteFeedback(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    },
    openShowDialog() {
      this.showForm = {}
      this.dialogShowFormVisible = true
      this.$refs.showForm && this.$refs.showForm.resetFields()
    },
    showFeedback(row) {
      queryAttach(row.rowId).then(({ data: { code, data } }) => {
        if (code === 0) {
          this.urls = data
        }

        this.openShowDialog()
        this.showForm = {
          rowId: row.rowId,
          appVersion: row.appVersion,
          clientOs: row.clientOs,
          appType: row.appType,
          clientType: row.clientType,
          classCode: row.classCode,
          typeCode: row.typeCode,
          resolution: row.resolution,
          questionDesc: row.questionDesc,
          answerDesc: row.answerDesc,
          remark: row.remark
        }
      })
    },
    openEditDialog() {
      this.editForm = {}
      this.dialogEditFormVisible = true
      this.$refs.editForm && this.$refs.editForm.resetFields()
    },
    editFeedback(row) {
      this.openEditDialog()
      this.editForm = {
        rowId: row.rowId,
        questionDesc: row.questionDesc,
        answerDesc: row.answerDesc
      }
    },
    execEditForm() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          answerFeedback(this.editForm).then(
            ({ data: { code, msg, data } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogEditFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
